package com.itxiaowu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itxiaowu.entity.PageResult;
import com.itxiaowu.entity.QueryPageBean;
import com.itxiaowu.entity.Result;
import com.itxiaowu.pojo.Permission;
import com.itxiaowu.pojo.Role;
import com.itxiaowu.pojo.User;
import com.itxiaowu.service.DyUserService;
import com.itxiaowu.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dyUser")
public class DyUserController {

    @Reference
    private DyUserService dyUserService;

    //分页
    @RequestMapping("/findPage.do")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = dyUserService.findPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;

    }

    //显示所有角色
    @RequestMapping("/findAllRole.do")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findAllRole() {
        try {
            List<Role> roleList = dyUserService.findAllRole();
            Result result = new Result(true, "角色查询成功!!", roleList);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色查询失败");
        }
    }

    //添加用户信息
    @RequestMapping("/add.do")
    @PreAuthorize("hasAuthority('USER_ADD')")
    public Result add(@RequestBody User user, Integer[] roleIds) throws Exception {
        //先判断日期是否大于当前日期
        if (user.getBirthday() != null) {
            //先判断用户生日日期不能大于今天
            String birthday3 = DateUtils.parseDate2String(user.getBirthday());
            String today = DateUtils.parseDate2String(DateUtils.getToday());
            String[] split = birthday3.split("-");
            String bb = "";
            for (String a : split) {
                bb += a;
            }
            String[] split2 = today.split("-");
            String tt = "";
            for (String a : split2) {
                tt += a;
            }
            int i = Integer.parseInt(bb);
            int i1 = Integer.parseInt(tt);
            //如果生日大于今天
            if (i >= i1) {
                return new Result(false, "你输入的日期大于今天");
            }
            //如果有并且日期正常
            //交给service处理
            dyUserService.addUser(user, roleIds);
            return new Result(true, "用户添加成功!");
        }
        //如果没有日期并且日期正常
        //交给service处理
        dyUserService.addUser(user, roleIds);
        return new Result(true, "用户添加成功!");
    }

    //根据id查询用户
    @RequestMapping("/queryUserById.do")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result queryUserById(Integer id) {
        try {
            User user = dyUserService.queryUserById(id);
            Date birthday = user.getBirthday();
            String s = DateUtils.parseDate2String(birthday, "yyyy-MM-dd");
            Map map = new HashMap<>();
            map.put("user", user);
            map.put("birthday", s);
            Result result = new Result(true, "用户查询成功", map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "用户查询失败");
        }
    }

    //查询所有权限信息
    @RequestMapping("/findAllPermission")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findAllPermission() {
        try {
            List<Role> permissionList = dyUserService.findAllPermission();
            Result result = new Result(true, "权限查询成功",permissionList);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "权限查询失败");
        }
    }
    //根据用户id 回显已经选中的角色id
    @RequestMapping("/findRoleIdsById")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findRoleIdsById(Integer id){
        try {
            Integer[] ids=dyUserService.findRoleIdsById(id);
            Result result = new Result(true,"角色查询成功",ids);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"角色查询失败!!");
        }
    }
    //编辑用户
    @RequestMapping("/edit.do")
    @PreAuthorize("hasAuthority('USER_EDIT')")
    public Result edit(@RequestBody User user,Integer[] roleIds){
        try {
            //都交给service处理
            dyUserService.edit(user,roleIds);
            return new Result(true,"用户修改成功!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户修改失败!!");
        }

    }
    //根据id删除用户
    @RequestMapping("/deleteUserById.do")
    @PreAuthorize("hasAuthority('USER_DELETE')")
    public Result deleteUserById(Integer id){
        try {
            dyUserService.deleteUserById(id);
            return new Result(true,"用户删除成功!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户删除失败!!!");
        }
    }
}
